package org.meiling.test;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.JFrame;

import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.meiling.util.Sounds;
import org.meiling.util.Utils;



public class Main extends JFrame implements java.awt.event.MouseListener,WindowListener,KeyListener{
	public Rect[][] rects = new Rect[10][10];
	public MouseEvent e =null;
	ArrayList<Rect> rectList = new ArrayList<Rect>();
	int lv = 1;
	int zongfen =Utils.needNum(lv);
	boolean guoguan = false;
	boolean shu = false;
	boolean cob1 =false;
	boolean cob2 =false;
	boolean cob3 =false;
	public int Score;  //得分
	public Main(){
		this.setTitle("消灭星星");
		new BufferedThread().start();
		this.setIconImage(Utils.tubiao.getImage());
		Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
		int locationX = (int)(dimension.getWidth()-Utils.WIDTH)/2;
		int locationY = (int)(dimension.getHeight()-Utils.HEIGHT)/2;
		this.setBounds(locationX,locationY,Utils.WIDTH,Utils.HEIGHT);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);
		this.addMouseListener(this);
		this.addWindowListener(this);
		ListenerThread lt = new ListenerThread();
		this.addKeyListener(this);
		lt.start();
		this.setVisible(true);
		new SetCob().start();
		
	}
	
	public void init(){
		File file = new File("./game.xml");
			Sounds.playStart();
			for (int i = 0; i < rects[0].length; i++) {
				for (int j = 0; j < rects[i].length; j++) {
					rects[i][j] = new Rect(i*Utils.RECT_WIDTH,j*Utils.RECT_HEIGHT);
				}
			}
			
			for (int i = rects[0].length-1; i >= 0; i--) {
				for (int j = rects[0].length-1; j >= 0; j--) {
					int y = rects[j][i].getY()+200;
					while(rects[j][i].getY()<y){
						rects[j][i].setY(rects[j][i].getY()+50);
						try {
							Thread.sleep(1);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					
				}
			}
	}

	public void returnInit(){ //继续游戏
		readMap();
	}
	@Override
	public void paint(Graphics g) {
		BufferedImage bi = new BufferedImage(Utils.WIDTH,Utils.HEIGHT,BufferedImage.TYPE_3BYTE_BGR);
		Graphics2D g2d = (Graphics2D) bi.getGraphics();
		Font f = new Font("微软雅黑",Font.BOLD,20);
		g2d.setFont(f);
		g2d.drawImage(Utils.bgGame.getImage(),0,0,this);
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.drawString("关卡"+lv,30,80);
		g2d.drawString("目标分数"+zongfen,180,80);
		f= new Font("微软雅黑",Font.BOLD,30);
		g2d.drawString(Score+"",230, 120);
		for (int i = 0; i < rects[0].length; i++) {
			for (int j = 0; j < rects[0].length; j++) {
				if (rects[i][j]!=null) {
					rects[i][j].paint(g2d);
				}
			}
		}
		if (guoguan) {
			g2d.drawImage(Utils.guoguan.getImage(),(Utils.WIDTH-Utils.guoguan.getImage().getWidth(null))/2,(Utils.HEIGHT-Utils.guoguan.getImage().getHeight(null))/2-30,this);
		}
		if (shu) {
			g2d.drawImage(Utils.gameover.getImage(),(Utils.WIDTH-Utils.gameover.getImage().getWidth(null))/2,(Utils.HEIGHT-Utils.gameover.getImage().getHeight(null))/2-30,this);
		}
		if (cob1) {
			g2d.drawImage(Utils.combo_1.getImage(),(Utils.WIDTH-Utils.combo_1.getImage().getWidth(null))/2,(Utils.HEIGHT-Utils.combo_1.getImage().getHeight(null))/2-20,this);
		}
		if (cob2) {
			g2d.drawImage(Utils.combo_2.getImage(),(Utils.WIDTH-Utils.combo_2.getImage().getWidth(null))/2,(Utils.HEIGHT-Utils.combo_2.getImage().getHeight(null))/2-20,this);
		}
		if (cob3) {
			g2d.drawImage(Utils.combo_3.getImage(),(Utils.WIDTH-Utils.combo_3.getImage().getWidth(null))/2,(Utils.HEIGHT-Utils.combo_3.getImage().getHeight(null))/2-20,this);
		}
		g.drawImage(bi, 10,10,this);
	}
	
		
	
	

	class ListenerThread extends Thread{
		@Override
		public  void run() {
			
			Rect index = null;
			int boob = 1;
			while(this.isAlive()){
				if (e!=null) {
					for (int i = 0; i < rects[0].length; i++) {
						for (int j = 0; j <rects[0].length; j++) {
							if (rects[i][j]!=null) {
								Rectangle rect2 = new Rectangle(rects[i][j].getX(),rects[i][j].getY(),Utils.RECT_WIDTH,Utils.RECT_HEIGHT);
								if (rect2.contains(new Point(e.getX(),e.getY()))) {
									if (rects[i][j].isClicked()) {
										boob++;
									}
									if (!rects[i][j].isClicked()) {
										rects[i][j].setClicked(true);
										boob = 1;
									}
									
									if (rects[i][j].isClicked()) {
										if (!rectList.contains(rects[i][j])) {
											for (Rect rect : rectList) {
												rect.setClicked(!rect.isClicked());
											}
											rectList.clear();
											rectList.add(rects[i][j]);
										}
									}
								}
							}
						}
					}
					System.out.println(boob);
					if (boob==3) {
						if (rectList.size()>1) {
							for (int jj = 0; jj < rectList.size(); jj++) {
								Rect r = rectList.get(jj);
								for (int i = 0; i < rects[0].length; i++) {
									for (int j = 0; j < rects[0].length; j++) {
										if (rects[i][j]!=null) {
											if (rects[i][j]==r) {
												rects[i][j]=null;
												Sounds.playClear();
											}
										}
									}
									try {
										Thread.sleep(3);
									} catch (InterruptedException e1) {
										e1.printStackTrace();
									}
								}
							}
							Score+=Utils.calc(rectList.size());
							if (rectList.size()==5) {
								cob1 = true;
								Sounds.playCob1();
							}else if (rectList.size()==6) {
								cob2 = true;
								Sounds.playCob2();
								try {
									Thread.sleep(150);
								} catch (InterruptedException e1) {
									e1.printStackTrace();
								}
							}else if (rectList.size()==7) {
								cob3 = true;
								Sounds.playCob3();
							}else if (rectList.size()>7){
								cob3 = true;
								Sounds.playCob3();
							}
							rectList.clear();
						}
						boob=1;
					}
	/********************************************!!!*************************************************/
					for (int i = 0; i < rects[0].length; i++) {
						for (int j = 0; j < 9; j++) {
							if (rects[i][j]!=null) {
								if (rects[i][j+1]==null) {
									rects[i][j].setY(rects[i][j].getY()+Utils.RECT_HEIGHT);
									rects[i][j+1]=rects[i][j];
									rects[i][j]=null;
									System.out.println("=============");
								}
							}
						}
					}
					for (int i = 0; i < rects[0].length; i++) {
						for (int j = 0; j < 9; j++) {
							if (rects[i][j]!=null) {
								if (rects[i][j+1]==null) {
									rects[i][j].setY(rects[i][j].getY()+Utils.RECT_HEIGHT);
									rects[i][j+1]=rects[i][j];
									rects[i][j]=null;
									System.out.println("=============");
								}
							}
						}
					}
					for (int i = 0; i < rects[0].length; i++) {
						for (int j = 0; j < 9; j++) {
							if (rects[i][j]!=null) {
								if (rects[i][j+1]==null) {
									rects[i][j].setY(rects[i][j].getY()+Utils.RECT_HEIGHT);
									rects[i][j+1]=rects[i][j];
									rects[i][j]=null;
									System.out.println("=============");
								}
							}
						}
					}
					for (int i = 0; i < rects[0].length; i++) {
						for (int j = 0; j < 9; j++) {
							if (rects[i][j]!=null) {
								if (rects[i][j+1]==null) {
									rects[i][j].setY(rects[i][j].getY()+Utils.RECT_HEIGHT);
									rects[i][j+1]=rects[i][j];
									rects[i][j]=null;
									System.out.println("=============");
								}
							}
						}
					}
					for (int i = 0; i < rects[0].length; i++) {
						for (int j = 0; j < 9; j++) {
							if (rects[i][j]!=null) {
								if (rects[i][j+1]==null) {
									rects[i][j].setY(rects[i][j].getY()+Utils.RECT_HEIGHT);
									rects[i][j+1]=rects[i][j];
									rects[i][j]=null;
									System.out.println("=============");
								}
							}
						}
					}
					for (int i = 0; i < rects[0].length; i++) {
						for (int j = 0; j < 9; j++) {
							if (rects[i][j]!=null) {
								if (rects[i][j+1]==null) {
									rects[i][j].setY(rects[i][j].getY()+Utils.RECT_HEIGHT);
									rects[i][j+1]=rects[i][j];
									rects[i][j]=null;
									System.out.println("=============");
								}
							}
						}
					}for (int i = 0; i < rects[0].length; i++) {
						for (int j = 0; j < 9; j++) {
							if (rects[i][j]!=null) {
								if (rects[i][j+1]==null) {
									rects[i][j].setY(rects[i][j].getY()+Utils.RECT_HEIGHT);
									rects[i][j+1]=rects[i][j];
									rects[i][j]=null;
									System.out.println("=============");
								}
							}
						}
					}for (int i = 0; i < rects[0].length; i++) {
						for (int j = 0; j < 9; j++) {
							if (rects[i][j]!=null) {
								if (rects[i][j+1]==null) {
									rects[i][j].setY(rects[i][j].getY()+Utils.RECT_HEIGHT);
									rects[i][j+1]=rects[i][j];
									rects[i][j]=null;
									System.out.println("=============");
								}
							}
						}
					}for (int i = 0; i < rects[0].length; i++) {
						for (int j = 0; j < 9; j++) {
							if (rects[i][j]!=null) {
								if (rects[i][j+1]==null) {
									rects[i][j].setY(rects[i][j].getY()+Utils.RECT_HEIGHT);
									rects[i][j+1]=rects[i][j];
									rects[i][j]=null;
									System.out.println("=============");
								}
							}
						}
					}for (int i = 0; i < rects[0].length; i++) {
						for (int j = 0; j < 9; j++) {
							if (rects[i][j]!=null) {
								if (rects[i][j+1]==null) {
									rects[i][j].setY(rects[i][j].getY()+Utils.RECT_HEIGHT);
									rects[i][j+1]=rects[i][j];
									rects[i][j]=null;
									System.out.println("=============");
								}
							}
						}
					}for (int i = 0; i < rects[0].length; i++) {
						for (int j = 0; j < 9; j++) {
							if (rects[i][j]!=null) {
								if (rects[i][j+1]==null) {
									rects[i][j].setY(rects[i][j].getY()+Utils.RECT_HEIGHT);
									rects[i][j+1]=rects[i][j];
									rects[i][j]=null;
									System.out.println("=============");
								}
							}
						}
					}
					
	/*******************************************************************************************************/				
					
					for (int i = 1; i < rects[0].length; i++) {
						int cc = 0;
						for (int j = 0; j <rects[0].length; j++) {
							if (i-1>=0) {
								if (rects[i-1][j]!=null) {
									cc++;
								}
							}
						}
						if (cc==0) {
							if (i-1>=0) {
								for (int j = 0; j < rects[0].length; j++) {
									if (rects[i][j]!=null) {
										rects[i][j].setX(rects[i][j].getX()-Utils.RECT_WIDTH);
										rects[i-1][j]=rects[i][j];
										rects[i][j]=null;
									}
								}
							}
						}
					}
					
					for (int i = 1; i < rects[0].length; i++) {
						int cc = 0;
						for (int j = 0; j <rects[0].length; j++) {
							if (i-1>=0) {
								if (rects[i-1][j]!=null) {
									cc++;
								}
							}
						}
						if (cc==0) {
							if (i-1>=0) {
								for (int j = 0; j < rects[0].length; j++) {
									if (rects[i][j]!=null) {
										rects[i][j].setX(rects[i][j].getX()-Utils.RECT_WIDTH);
										rects[i-1][j]=rects[i][j];
										rects[i][j]=null;
									}
								}
							}
						}
					}
					for (int i = 1; i < rects[0].length; i++) {
						int cc = 0;
						for (int j = 0; j <rects[0].length; j++) {
							if (i-1>=0) {
								if (rects[i-1][j]!=null) {
									cc++;
								}
							}
						}
						if (cc==0) {
							if (i-1>=0) {
								for (int j = 0; j < rects[0].length; j++) {
									if (rects[i][j]!=null) {
										rects[i][j].setX(rects[i][j].getX()-Utils.RECT_WIDTH);
										rects[i-1][j]=rects[i][j];
										rects[i][j]=null;
									}
								}
							}
						}
					}
					for (int i = 1; i < rects[0].length; i++) {
						int cc = 0;
						for (int j = 0; j <rects[0].length; j++) {
							if (i-1>=0) {
								if (rects[i-1][j]!=null) {
									cc++;
								}
							}
						}
						if (cc==0) {
							if (i-1>=0) {
								for (int j = 0; j < rects[0].length; j++) {
									if (rects[i][j]!=null) {
										rects[i][j].setX(rects[i][j].getX()-Utils.RECT_WIDTH);
										rects[i-1][j]=rects[i][j];
										rects[i][j]=null;
									}
								}
							}
						}
					}
					for (int i = 1; i < rects[0].length; i++) {
						int cc = 0;
						for (int j = 0; j <rects[0].length; j++) {
							if (i-1>=0) {
								if (rects[i-1][j]!=null) {
									cc++;
								}
							}
						}
						if (cc==0) {
							if (i-1>=0) {
								for (int j = 0; j < rects[0].length; j++) {
									if (rects[i][j]!=null) {
										rects[i][j].setX(rects[i][j].getX()-Utils.RECT_WIDTH);
										rects[i-1][j]=rects[i][j];
										rects[i][j]=null;
									}
								}
							}
						}
					}
					for (int i = 1; i < rects[0].length; i++) {
						int cc = 0;
						for (int j = 0; j <rects[0].length; j++) {
							if (i-1>=0) {
								if (rects[i-1][j]!=null) {
									cc++;
								}
							}
						}
						if (cc==0) {
							if (i-1>=0) {
								for (int j = 0; j < rects[0].length; j++) {
									if (rects[i][j]!=null) {
										rects[i][j].setX(rects[i][j].getX()-Utils.RECT_WIDTH);
										rects[i-1][j]=rects[i][j];
										rects[i][j]=null;
									}
								}
							}
						}
					}
					for (int i = 1; i < rects[0].length; i++) {
						int cc = 0;
						for (int j = 0; j <rects[0].length; j++) {
							if (i-1>=0) {
								if (rects[i-1][j]!=null) {
									cc++;
								}
							}
						}
						if (cc==0) {
							if (i-1>=0) {
								for (int j = 0; j < rects[0].length; j++) {
									if (rects[i][j]!=null) {
										rects[i][j].setX(rects[i][j].getX()-Utils.RECT_WIDTH);
										rects[i-1][j]=rects[i][j];
										rects[i][j]=null;
									}
								}
							}
						}
					}
					for (int i = 1; i < rects[0].length; i++) {
						int cc = 0;
						for (int j = 0; j <rects[0].length; j++) {
							if (i-1>=0) {
								if (rects[i-1][j]!=null) {
									cc++;
								}
							}
						}
						if (cc==0) {
							if (i-1>=0) {
								for (int j = 0; j < rects[0].length; j++) {
									if (rects[i][j]!=null) {
										rects[i][j].setX(rects[i][j].getX()-Utils.RECT_WIDTH);
										rects[i-1][j]=rects[i][j];
										rects[i][j]=null;
									}
								}
							}
						}
					}
					for (int i = 1; i < rects[0].length; i++) {
						int cc = 0;
						for (int j = 0; j <rects[0].length; j++) {
							if (i-1>=0) {
								if (rects[i-1][j]!=null) {
									cc++;
								}
							}
						}
						if (cc==0) {
							if (i-1>=0) {
								for (int j = 0; j < rects[0].length; j++) {
									if (rects[i][j]!=null) {
										rects[i][j].setX(rects[i][j].getX()-Utils.RECT_WIDTH);
										rects[i-1][j]=rects[i][j];
										rects[i][j]=null;
									}
								}
							}
						}
					}
					for (int i = 1; i < rects[0].length; i++) {
						int cc = 0;
						for (int j = 0; j <rects[0].length; j++) {
							if (i-1>=0) {
								if (rects[i-1][j]!=null) {
									cc++;
								}
							}
						}
						if (cc==0) {
							if (i-1>=0) {
								for (int j = 0; j < rects[0].length; j++) {
									if (rects[i][j]!=null) {
										rects[i][j].setX(rects[i][j].getX()-Utils.RECT_WIDTH);
										rects[i-1][j]=rects[i][j];
										rects[i][j]=null;
									}
								}
							}
						}
					}
					for (int i = 1; i < rects[0].length; i++) {
						int cc = 0;
						for (int j = 0; j <rects[0].length; j++) {
							if (i-1>=0) {
								if (rects[i-1][j]!=null) {
									cc++;
								}
							}
						}
						if (cc==0) {
							if (i-1>=0) {
								for (int j = 0; j < rects[0].length; j++) {
									if (rects[i][j]!=null) {
										rects[i][j].setX(rects[i][j].getX()-Utils.RECT_WIDTH);
										rects[i-1][j]=rects[i][j];
										rects[i][j]=null;
									}
								}
							}
						}
					}
					
					
		/************************************************************************************************************/			
					
					for (int ii = 0; ii < rectList.size(); ii++) {
							Rect indexs= rectList.get(ii);
						for (int i = 0; i < rects[0].length; i++) {
							for (int j = 0; j < rects[0].length; j++) {
								if (indexs==rects[i][j]){
									if (i+1>=0&&i+1<=9) {
										if (rects[i+1][j]!=null) {
											if (rects[i+1][j].getType()==indexs.getType()) {
												if (!rectList.contains(rects[i+1][j])) {
													rects[i+1][j].setClicked(true);
													rectList.add(rects[i+1][j]);
												}
											}
										}
									}
									if (i-1>=0&&i-1<=9) {
										if (rects[i-1][j]!=null) {
											if (rects[i-1][j].getType()==indexs.getType()) {
												if (!rectList.contains(rects[i-1][j])) {
													rects[i-1][j].setClicked(true);
													rectList.add(rects[i-1][j]);
												}
											}
										}
									}
									if (j-1>=0&&j-1<=9) {
										if (rects[i][j-1]!=null) {
											if (rects[i][j-1].getType()==indexs.getType()) {
												if (!rectList.contains(rects[i][j-1])) {
													rects[i][j-1].setClicked(true);
													rectList.add(rects[i][j-1]);
												}
											}
										}
									}
									if (j+1>=0&&j+1<=9) {
										if (rects[i][j+1]!=null) {
											if (rects[i][j+1].getType()==indexs.getType()) {
												if (!rectList.contains(rects[i][j+1])) {
													rects[i][j+1].setClicked(true);
													rectList.add(rects[i][j+1]);
												}
											}
										}
									}
								}
							}
						}
					}
/***********************************************ͬ皆为智障方法，请勿使用!!!*************************************************************/
					int aaa  = 0;
					for (int ii = 0; ii < rects[0].length; ii++) {
						for (int jj = 0; jj < rects[0].length; jj++) {
							if (rects[ii][jj]!=null) {
								Rect indexs = rects[ii][jj];
								for (int i = 0; i < rects[0].length; i++) {
									for (int j = 0; j < rects[0].length; j++) {
										if (indexs==rects[i][j]){
											if (i+1>=0&&i+1<=9) {
												if (rects[i+1][j]!=null) {
													if (rects[i+1][j].getType()==indexs.getType()) {
														aaa++;
													}
												}
											}
											if (i-1>=0&&i-1<=9) {
												if (rects[i-1][j]!=null) {
													if (rects[i-1][j].getType()==indexs.getType()) {
														aaa++;
													}
												}
											}
											if (j-1>=0&&j-1<=9) {
												if (rects[i][j-1]!=null) {
													if (rects[i][j-1].getType()==indexs.getType()) {
														aaa++;
													}
												}
											}
											if (j+1>=0&&j+1<=9) {
												if (rects[i][j+1]!=null) {
													if (rects[i][j+1].getType()==indexs.getType()) {
														aaa++;
													}
												}
											}
										}
									}
								}
							}
						}
					}
				
					if (aaa==0) {
						int aabc = 0;
						for (int i = 0; i < rects[0].length; i++) {
							for (int j = 0; j < rects[0].length; j++) {
								if (rects[i][j]!=null) {
									aabc++;
								}
							}
						}
						if (aabc<10) {
							int ssa = Score+=2000-aabc*5;
							while(Score<ssa) {
								Score+=20;
								try {
									Thread.sleep(50);
								} catch (InterruptedException e1) {
									e1.printStackTrace();
								}
							}
						}
						
						
						if (Score>=zongfen) {
							guoguan=true;
							Sounds.playNext();
							lv++;
							zongfen = Utils.needNum(lv);
							init();
							try {
								Thread.sleep(50);
							} catch (InterruptedException e1) {
								e1.printStackTrace();
							}
							guoguan=false;
						}else{
							Sounds.playShu();
							shu=true;
							try {
								Thread.sleep(2000);
								shu=false;
								dispose();
								new FirstMain();
							} catch (InterruptedException e1) {
								e1.printStackTrace();
							}
						}
					}
					try {
						Thread.sleep(70);
					} catch (InterruptedException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
				
			}
		}	
	}

	class SetCob extends Thread{
		@Override
		public void run() {
			while(true){
				System.out.print("");
				if (cob1) {
					try {
						Thread.sleep(250);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					cob1=false;
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					cob1=true;
					try {
						Thread.sleep(250);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					cob1=false;
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					cob1=true;
					try {
						Thread.sleep(250);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					cob1=false;
				}
				if (cob2){
					try {
						Thread.sleep(250);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					cob2=false;
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					cob2=true;
					try {
						Thread.sleep(250);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					cob2=false;
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					cob2=true;
					try {
						Thread.sleep(250);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					cob2=false;
				}
				if (cob3) {
					try {
						Thread.sleep(250);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					cob3=false;
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					cob3=true;
					try {
						Thread.sleep(250);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					cob3=false;
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					cob3=true;
					try {
						Thread.sleep(250);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					cob3=false;
				}
			}
		}
	}
	
	class BufferedThread extends Thread{
		@Override
		public void run() {
			while(true){
				repaint();
				try {
					Thread.sleep(50);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	

	@Override
	public void mousePressed(MouseEvent e) {
		
		this.e = e;
		System.out.println("aa");
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		
	}

	
	@Override
	public void mouseReleased(MouseEvent e) {
		this.e = null;
	}

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {	
	}

	@Override
	public void windowOpened(WindowEvent e) {
		
	}

	@Override
	public void windowClosing(WindowEvent e) {
		System.out.println("===========");
		saveMap();
	}

	@Override
	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowActivated(WindowEvent e) {
	
	}
	
	/**
	 * <rect>
	 * <x></x><y></y><iskong></iskong><type></type>
	 * </rect>
	 * 
	 */
	
	public void saveMap(){

		File file = new File("./game.xml");
		if (shu) {
			file.delete();
			return;
		}
		try {
		file.createNewFile();
		String str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
		str+="<rects lv=\""+lv+"\" count=\""+Score+"\">";
		for (int i = 0; i < rects[0].length; i++) {
			for (int j = 0; j < rects[0].length; j++) {
				Rect rect =rects[i][j];
				if (rect==null) {
					rect = new Rect(i*Utils.RECT_WIDTH,i*Utils.RECT_HEIGHT,1,null);
				}else{
					rect = rects[i][j];
				}
				rect.i=i;
				rect.j=j;
				str+="<rect>";
				str+="<x>"+rect.getX()+"</x>";
				str+="<y>"+rect.getY()+"</y>";
				str+="<i>"+rect.i+"</i>";
				str+="<j>"+rect.j+"</j>";
				str+="<iskong>"+rect.iskong+"</iskong>";
				str+="<type>"+rect.getType()+"</type>";
				str+="</rect>";
				str+="\r\n";
			}
		}
		str+="</rects>";
		BufferedWriter bw = new BufferedWriter(new FileWriter(file));
		bw.write(str);
		bw.flush();
		bw.close();
		System.out.println("写入成功!");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	
	public  void readMap(){
		Sounds.playStart();
		File file = new File("./game.xml");
		if (file.isFile()) {
			try {
				org.dom4j.Document doc = new SAXReader().read(file);
				Element root = doc.getRootElement();
				lv = Integer.parseInt(root.attributeValue("lv"));
				Score = Integer.parseInt(root.attributeValue("count"));
				zongfen=Utils.needNum(lv);
				List<Element> lists = root.elements();
				ArrayList<Rect> rectLists = new ArrayList<Rect>();
				for (Element e : lists) {
					Rect r = new Rect();
					List<Element> lls = e.elements();
					r.setX(Integer.parseInt(lls.get(0).getText()));
					r.setY(Integer.parseInt(lls.get(1).getText()));
					r.i=Integer.parseInt(lls.get(2).getText());
					r.j=Integer.parseInt(lls.get(3).getText());
					r.iskong = Integer.parseInt(lls.get(4).getText());
					r.setType(Integer.parseInt(lls.get(5).getText()));
					r.setShow();
					rectLists.add(r);
				}
				System.out.println(rectLists.size()+"------------------");
				for (Rect rect : rectLists) {
					rects[rect.i][rect.j] = rect;
				}
				for (int i = 0; i < rects[0].length; i++) {
					for (int j = 0; j < rects[0].length; j++) {
						if (rects[i][j].iskong==1) {
							rects[i][j]=null;
						}
					}
				}
				
			} catch (DocumentException e) {
				e.printStackTrace();
			}
			
		}
	}
	@Override
	public void windowDeactivated(WindowEvent e) {
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// 
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
		Random rand = new Random();
		int b = rand.nextInt(5);
		switch (e.getKeyCode()) {
		case KeyEvent.VK_0:
			for (Rect r: rectList) {
				r.setType(0);
				r.setShow();
			}
			break;
		case KeyEvent.VK_1:
			for (Rect r: rectList) {
				r.setType(1);
				r.setShow();
			}	
					break;	
		case KeyEvent.VK_2:
			for (Rect r: rectList) {
				r.setType(2);
				r.setShow();
			}
			break;
		case KeyEvent.VK_3:
			for (Rect r: rectList) {
				r.setType(3);
				r.setShow();
			}
			break;
		case KeyEvent.VK_4:
			for (Rect r: rectList) {
				r.setType(4);
				r.setShow();
			}
			break;
		}
		
		if (e.getKeyCode()==KeyEvent.VK_S) {   //更改颜色
			for (Rect r: rectList) {
				r.setType(b);
				r.setShow();
			}
		}
		if (e.getKeyCode()==KeyEvent.VK_D) {
			for (Rect t : rectList) {
				for (int i = 0; i < rects[0].length; i++) {
					for (int j = 0; j <rects[0].length; j++) {
						if (rects[i][j]!=null) {
							if (t==rects[i][j]) {
								rects[i][j]=null;
							}
						}
					}
				}
			}
			rectList.clear();
			
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

}

